{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Edit Your Expectation Suite\n",
    "Use this notebook to recreate and modify your expectation suite:\n",
    "\n",
    "**Expectation Suite Name**: `basic.warning`\n",
    "\n",
    "We'd love it if you **reach out to us on** the [**Great Expectations Slack Channel**](https://greatexpectations.io/slack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-06-24T09:51:24-0400 - INFO - Great Expectations logging enabled at 20 level by JupyterUX module.\n"
     ]
    },
    {
     "ename": "DataContextError",
     "evalue": "expectation_suite basic.warning not found",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mDataContextError\u001b[0m                          Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-417a61f5b6f4>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     13\u001b[0m \u001b[1;31m# remove the other one.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     14\u001b[0m \u001b[0mexpectation_suite_name\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"basic.warning\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 15\u001b[1;33m \u001b[0msuite\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcontext\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_expectation_suite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mexpectation_suite_name\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     16\u001b[0m \u001b[0msuite\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexpectations\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     17\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\leorf\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\great_expectations\\data_context\\data_context.py\u001b[0m in \u001b[0;36mget_expectation_suite\u001b[1;34m(self, expectation_suite_name)\u001b[0m\n\u001b[0;32m   1154\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1155\u001b[0m             raise ge_exceptions.DataContextError(\n\u001b[1;32m-> 1156\u001b[1;33m                 \u001b[1;34m\"expectation_suite %s not found\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0mexpectation_suite_name\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1157\u001b[0m             )\n\u001b[0;32m   1158\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mDataContextError\u001b[0m: expectation_suite basic.warning not found"
     ],
     "output_type": "error"
    }
   ],
   "source": [
    "import dagstermill\n",
    "import json\n",
    "import datetime\n",
    "import great_expectations as ge\n",
    "import great_expectations.jupyter_ux\n",
    "from great_expectations.data_context.types.resource_identifiers import (\n",
    "    ValidationResultIdentifier,\n",
    ")\n",
    "\n",
    "context = ge.data_context.DataContext()\n",
    "\n",
    "# Feel free to change the name of your suite here. Renaming this will not\n",
    "# remove the other one.\n",
    "expectation_suite_name = \"basic.warning\"\n",
    "suite = context.get_expectation_suite(expectation_suite_name)\n",
    "suite.expectations = []\n",
    "\n",
    "batch_kwargs = {\n",
    "    \"data_asset_name\": \"basic\",\n",
    "    \"datasource\": \"blink\",\n",
    "    \"path\": \"C:\\\\Users\\\\leorf\\\\PycharmProjects\\\\elementl\\\\dagster\\\\python_modules\\\\libraries\\\\dagster-ge\\\\great_expectations\\\\..\\\\.\\\\basic.csv\",\n",
    "}\n",
    "batch = context.get_batch(batch_kwargs, suite)\n",
    "batch.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create & Edit Expectations\n",
    "\n",
    "Add expectations by calling specific expectation methods on the `batch` object. They all begin with `.expect_` which makes autocompleting easy using tab.\n",
    "\n",
    "You can see all the available expectations in the **[expectation glossary](https://docs.greatexpectations.io/en/latest/reference/glossary_of_expectations.html?utm_source=notebook&utm_medium=create_expectations)**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Table Expectation(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch.expect_table_row_count_to_be_between(max_value=33, min_value=27)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch.expect_table_column_count_to_equal(value=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch.expect_table_columns_to_match_ordered_list(\n",
    "    column_list=[\"Team\", ' \"Payroll (millions)\"', ' \"Wins\"']\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Column Expectation(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ` \"Payroll (millions)\"`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch.expect_column_values_to_not_be_null(' \"Payroll (millions)\"')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch.expect_column_min_to_be_between(\n",
    "    ' \"Payroll (millions)\"', max_value=56.24, min_value=54.24\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch.expect_column_max_to_be_between(\n",
    "    ' \"Payroll (millions)\"', max_value=198.96, min_value=196.96\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch.expect_column_mean_to_be_between(\n",
    "    ' \"Payroll (millions)\"', max_value=99.01899999999998, min_value=97.01899999999998\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch.expect_column_median_to_be_between(\n",
    "    ' \"Payroll (millions)\"', max_value=86.75, min_value=84.75\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch.expect_column_quantile_values_to_be_between(\n",
    "    ' \"Payroll (millions)\"',\n",
    "    allow_relative_error=False,\n",
    "    quantile_ranges={\n",
    "        \"quantiles\": [0.05, 0.25, 0.5, 0.75, 0.95],\n",
    "        \"value_ranges\": [\n",
    "            [54.37, 56.37],\n",
    "            [74.48, 76.48],\n",
    "            [82.31, 84.31],\n",
    "            [116.62, 118.62],\n",
    "            [173.54, 175.54],\n",
    "        ],\n",
    "    },\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `Team`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch.expect_column_values_to_not_be_null(\"Team\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch.expect_column_value_lengths_to_be_between(\"Team\", min_value=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Save & Review Your Expectations\n",
    "\n",
    "Let's save the expectation suite as a JSON file in the `great_expectations/expectations` directory of your project.\n",
    "If you decide not to save some expectations that you created, use [remove_expectation method](https://docs.greatexpectations.io/en/latest/module_docs/data_asset_module.html?highlight=remove_expectation&utm_source=notebook&utm_medium=edit_expectations#great_expectations.data_asset.data_asset.DataAsset.remove_expectation).\n",
    "\n",
    "Let's now rebuild your Data Docs, which helps you communicate about your data with both machines and humans."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch.save_expectation_suite(discard_failed_expectations=False)\n",
    "\n",
    "\"\"\"\n",
    "Let's create a run_id. The run_id must be of type RunIdentifier, with optional run_name and run_time instantiation\n",
    "arguments (or a dictionary with these keys). The run_name can be any string (this could come from your pipeline\n",
    "runner, e.g. Airflow run id). The run_time can be either a dateutil parsable string or a datetime object.\n",
    "Note - any provided datetime will be assumed to be a UTC time. If no instantiation arguments are given, run_name will\n",
    "be None and run_time will default to the current UTC datetime.\n",
    "\"\"\"\n",
    "\n",
    "run_id = {\n",
    "  \"run_name\": \"some_string_that_uniquely_identifies_this_run\",  # insert your own run_name here\n",
    "  \"run_time\": datetime.datetime.now(datetime.timezone.utc)\n",
    "}\n",
    "results = context.run_validation_operator(\"action_list_operator\", assets_to_validate=[batch], run_id=run_id)\n",
    "dagstermill.yield_result(json.loads(str(results.list_validation_results())))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dagster",
   "language": "python",
   "name": "dagster"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
